{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5760f3c8",
   "metadata": {},
   "source": [
    "## Conditional Pipeline using PandasQuery\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9e5485c0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\n",
      "kind: Model\n",
      "metadata:\n",
      "  name: choice-is-one\n",
      "spec:\n",
      "  storageUri: \"gs://seldon-models/scv2/examples/pandasquery\"\n",
      "  requirements:\n",
      "  - mlserver\n",
      "  - python\n",
      "  parameters:\n",
      "  - name: query\n",
      "    value: \"choice == 1\"\n",
      "---\n",
      "apiVersion: mlops.seldon.io/v1alpha1\n",
      "kind: Model\n",
      "metadata:\n",
      "  name: choice-is-two\n",
      "spec:\n",
      "  storageUri: \"gs://seldon-models/scv2/examples/pandasquery\"\n",
      "  requirements:\n",
      "  - mlserver\n",
      "  - python\n",
      "  parameters:\n",
      "  - name: query\n",
      "    value: \"choice == 2\"\n",
      "---\n",
      "apiVersion: mlops.seldon.io/v1alpha1\n",
      "kind: Model\n",
      "metadata:\n",
      "  name: add10\n",
      "spec:\n",
      "  storageUri: \"gs://seldon-models/scv2/samples/triton_23-03/add10\"\n",
      "  requirements:\n",
      "  - triton\n",
      "  - python\n",
      "---\n",
      "apiVersion: mlops.seldon.io/v1alpha1\n",
      "kind: Model\n",
      "metadata:\n",
      "  name: mul10\n",
      "spec:\n",
      "  storageUri: \"gs://seldon-models/scv2/samples/triton_23-03/mul10\"\n",
      "  requirements:\n",
      "  - triton\n",
      "  - python\n"
     ]
    }
   ],
   "source": [
    "!cat ../../models/choice1.yaml\n",
    "!echo \"---\"\n",
    "!cat ../../models/choice2.yaml\n",
    "!echo \"---\"\n",
    "!cat ../../models/add10.yaml\n",
    "!echo \"---\"\n",
    "!cat ../../models/mul10.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c31d41c1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n",
      "{}\n",
      "{}\n",
      "{}\n"
     ]
    }
   ],
   "source": [
    "!seldon model load -f ../../models/choice1.yaml\n",
    "!seldon model load -f ../../models/choice2.yaml\n",
    "!seldon model load -f ../../models/add10.yaml\n",
    "!seldon model load -f ../../models/mul10.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "cb503272",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n",
      "{}\n",
      "{}\n",
      "{}\n"
     ]
    }
   ],
   "source": [
    "!seldon model status choice-is-one -w ModelAvailable \n",
    "!seldon model status choice-is-two -w ModelAvailable \n",
    "!seldon model status add10 -w ModelAvailable \n",
    "!seldon model status mul10 -w ModelAvailable "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "36300582",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Pipeline\r\n",
      "metadata:\r\n",
      "  name: choice\r\n",
      "spec:\r\n",
      "  steps:\r\n",
      "  - name: choice-is-one\r\n",
      "  - name: mul10\r\n",
      "    inputs:\r\n",
      "    - choice.inputs.INPUT\r\n",
      "    triggers:\r\n",
      "    - choice-is-one.outputs.choice\r\n",
      "  - name: choice-is-two\r\n",
      "  - name: add10\r\n",
      "    inputs:\r\n",
      "    - choice.inputs.INPUT\r\n",
      "    triggers:\r\n",
      "    - choice-is-two.outputs.choice\r\n",
      "  output:\r\n",
      "    steps:\r\n",
      "    - mul10\r\n",
      "    - add10\r\n",
      "    stepsJoin: any\r\n"
     ]
    }
   ],
   "source": [
    "!cat ../../pipelines/choice.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ba8bd865",
   "metadata": {},
   "outputs": [],
   "source": [
    "!seldon pipeline load -f ../../pipelines/choice.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "50f88343",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "  \"pipelineName\": \"choice\",\r\n",
      "  \"versions\": [\r\n",
      "    {\r\n",
      "      \"pipeline\": {\r\n",
      "        \"name\": \"choice\",\r\n",
      "        \"uid\": \"cifel9aufmbc73e5intg\",\r\n",
      "        \"version\": 1,\r\n",
      "        \"steps\": [\r\n",
      "          {\r\n",
      "            \"name\": \"add10\",\r\n",
      "            \"inputs\": [\r\n",
      "              \"choice.inputs.INPUT\"\r\n",
      "            ],\r\n",
      "            \"triggers\": [\r\n",
      "              \"choice-is-two.outputs.choice\"\r\n",
      "            ]\r\n",
      "          },\r\n",
      "          {\r\n",
      "            \"name\": \"choice-is-one\"\r\n",
      "          },\r\n",
      "          {\r\n",
      "            \"name\": \"choice-is-two\"\r\n",
      "          },\r\n",
      "          {\r\n",
      "            \"name\": \"mul10\",\r\n",
      "            \"inputs\": [\r\n",
      "              \"choice.inputs.INPUT\"\r\n",
      "            ],\r\n",
      "            \"triggers\": [\r\n",
      "              \"choice-is-one.outputs.choice\"\r\n",
      "            ]\r\n",
      "          }\r\n",
      "        ],\r\n",
      "        \"output\": {\r\n",
      "          \"steps\": [\r\n",
      "            \"mul10.outputs\",\r\n",
      "            \"add10.outputs\"\r\n",
      "          ],\r\n",
      "          \"stepsJoin\": \"ANY\"\r\n",
      "        },\r\n",
      "        \"kubernetesMeta\": {}\r\n",
      "      },\r\n",
      "      \"state\": {\r\n",
      "        \"pipelineVersion\": 1,\r\n",
      "        \"status\": \"PipelineReady\",\r\n",
      "        \"reason\": \"created pipeline\",\r\n",
      "        \"lastChangeTimestamp\": \"2023-06-30T14:45:57.284684328Z\",\r\n",
      "        \"modelsReady\": true\r\n",
      "      }\r\n",
      "    }\r\n",
      "  ]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon pipeline status choice -w PipelineReady | jq -M ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "209c00c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "  \"outputs\": [\r\n",
      "    {\r\n",
      "      \"name\": \"OUTPUT\",\r\n",
      "      \"datatype\": \"FP32\",\r\n",
      "      \"shape\": [\r\n",
      "        \"4\"\r\n",
      "      ],\r\n",
      "      \"contents\": {\r\n",
      "        \"fp32Contents\": [\r\n",
      "          50,\r\n",
      "          60,\r\n",
      "          70,\r\n",
      "          80\r\n",
      "        ]\r\n",
      "      }\r\n",
      "    }\r\n",
      "  ]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon pipeline infer choice --inference-mode grpc \\\n",
    " '{\"model_name\":\"choice\",\"inputs\":[{\"name\":\"choice\",\"contents\":{\"int_contents\":[1]},\"datatype\":\"INT32\",\"shape\":[1]},{\"name\":\"INPUT\",\"contents\":{\"fp32_contents\":[5,6,7,8]},\"datatype\":\"FP32\",\"shape\":[4]}]}' | jq -M ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "26fab83d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "  \"outputs\": [\r\n",
      "    {\r\n",
      "      \"name\": \"OUTPUT\",\r\n",
      "      \"datatype\": \"FP32\",\r\n",
      "      \"shape\": [\r\n",
      "        \"4\"\r\n",
      "      ],\r\n",
      "      \"contents\": {\r\n",
      "        \"fp32Contents\": [\r\n",
      "          15,\r\n",
      "          16,\r\n",
      "          17,\r\n",
      "          18\r\n",
      "        ]\r\n",
      "      }\r\n",
      "    }\r\n",
      "  ]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon pipeline infer choice --inference-mode grpc \\\n",
    " '{\"model_name\":\"choice\",\"inputs\":[{\"name\":\"choice\",\"contents\":{\"int_contents\":[2]},\"datatype\":\"INT32\",\"shape\":[1]},{\"name\":\"INPUT\",\"contents\":{\"fp32_contents\":[5,6,7,8]},\"datatype\":\"FP32\",\"shape\":[4]}]}' | jq -M ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a2d69d25",
   "metadata": {},
   "outputs": [],
   "source": [
    "!seldon model unload choice-is-one\n",
    "!seldon model unload choice-is-two\n",
    "!seldon model unload add10\n",
    "!seldon model unload mul10\n",
    "!seldon pipeline unload choice"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ba3ba4ba",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
